package at.fht.bwi.sem5.map;

import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.content.res.XmlResourceParser;
import at.fht.bwi.sem5.domain.Address;
import at.fht.bwi.sem5.domain.Buschenschank;
import at.fht.bwi.sem5.domain.Coordinates;

public class BuschenschankReader {
	
	public static List<Buschenschank> readBuschenschaenke(XmlResourceParser parser){
		
		
		List<Buschenschank> buschenschankList = null;
		
		
		try {
			String elementName = null;
			
			String name = null;
			String street = null;
			String postalCode = null;
			String city = null;
			String phoneNumber = null;
			String latitude = null;
			String longitude = null;
			String homepage = null;
			
			Buschenschank buschenschank = null;
			Address address = null;
			Coordinates coordinates = null;

				try {
					
					int eventType = parser.getEventType();
					 
				    while (eventType != XmlPullParser.END_DOCUMENT) {
				       
				    	switch (eventType){
				    	
				    				case XmlPullParser.START_DOCUMENT:
				                        buschenschankList = new ArrayList<Buschenschank>();
				                        break;
				    				case XmlPullParser.START_TAG:
				    					elementName = parser.getName();
				    					if(elementName.equalsIgnoreCase("buschenschank")){
				    						//new buschenschank 
				    						buschenschank = new Buschenschank();
				    					}else if(elementName.equalsIgnoreCase("name")){
				    						name = parser.nextText();
				    					}else if(elementName.equalsIgnoreCase("strasse")){
				    						street = parser.nextText();
				    					}else if(elementName.equalsIgnoreCase("plz")){
				    						postalCode = parser.nextText();
				    					}else if(elementName.equalsIgnoreCase("ort")){
				    						city = parser.nextText();
				    					}else if(elementName.equalsIgnoreCase("telefonnummer")){
				    						phoneNumber = parser.nextText();
				    					}else if(elementName.equalsIgnoreCase("breitengrad")){
				    						latitude = parser.nextText();
				    					}else if(elementName.equalsIgnoreCase("laengengrad")){
				    						longitude = parser.nextText();
				    					}else if(elementName.equalsIgnoreCase("homepage")){
				    						homepage = parser.nextText();
				    					}
				    					break;
				    				case XmlPullParser.END_TAG:
				    					elementName = parser.getName();
				                        if(elementName.equalsIgnoreCase("buschenschank")){
				                        	//building a buschenschank
				                        	address = new Address(street, postalCode, city);
				                        	coordinates = new Coordinates(Double.parseDouble(latitude), Double.parseDouble(longitude));
				                        	buschenschank.setAddress(address);
				                        	buschenschank.setCoordinates(coordinates);
				                        	buschenschank.setName(name);
				                        	buschenschank.setPhoneNumber(phoneNumber);
				                        	buschenschank.setHomepage(homepage);
				                        	
				                        	//adding them to the list
				                        	buschenschankList.add(buschenschank);
				                        }
				                        break;
				        }
				    	eventType = parser.next();
				    }
				}
				catch (XmlPullParserException e) {
				    throw new RuntimeException("Cannot parse XML");
				}
				finally {
				    parser.close();
				}
				
		}catch(Exception e){
			e.printStackTrace();
		}
		return buschenschankList;
	}

}
